package com.visionork.android.tai.activity;

import java.util.List;

import com.visionork.android.tai.TaiApp;
import com.visionork.android.tai.model.Episode;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NewEpisodeListActivity extends ListActivity {
	protected EpisodeItemAdapter itemAdapter;

	protected class EpisodeItemAdapter extends ArrayAdapter<Episode> {
		public EpisodeItemAdapter(Context context, int textViewResourceId,
				List<Episode> objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.newepisode_list_item, null);
			}

			Episode epi = getItem(position);

			TextView tvTitle = (TextView) v.findViewById(R.id.title);
			tvTitle.setText(epi.getTitle());

			return v;
		}
	}

	protected class LoadEpisodeTask extends AsyncTask<Void, Void, Void> {
		private boolean useCache;
		protected ProgressDialog pd;

		public LoadEpisodeTask(boolean useCache) {
			this.useCache = useCache;
		}

		@Override
		protected Void doInBackground(Void... params) {
			TaiApp.getLatestEpisodes();
			if (!useCache || TaiApp.getLatestEpisodes().size() == 0) {
				TaiApp.getLatestEpisodes(false);
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			pd = ProgressDialog.show(NewEpisodeListActivity.this, null,
					res.getString(R.string.load), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			itemAdapter.notifyDataSetChanged();
			pd.dismiss();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newepisode_list);
		itemAdapter = new EpisodeItemAdapter(NewEpisodeListActivity.this,
				R.layout.newepisode_list_item, TaiApp.getLatestEpisodesRef());
		setListAdapter(itemAdapter);
		registerForContextMenu(getListView());
		loadData(true);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.newlist_context_menu, menu);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.newlist_option_menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.refresh:
			loadData(false);
			return true;
		case R.id.share:
			Episode epi = TaiApp.getLatestEpisodes().get(info.position);
			String subject = "FWD: " + epi.getTitle();
			String text = epi.getTitle() + " - " + epi.getUrlLink();
			TaiApp.share(this, subject, text);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Episode epi = TaiApp.getLatestEpisodes().get(position);
		Intent intent = new Intent(this, EpisodeActivityImpl.class);

		intent.putExtra(EpisodeActivityBase.KEY_DYNAMIC, true);
		intent.putExtra(EpisodeActivityBase.KEY_EPISODE_URL, epi.getUrlLink());

		startActivity(intent);
	}

	protected void loadData(boolean useCache) {
		LoadEpisodeTask task = new LoadEpisodeTask(useCache);
		task.execute(new Void[] {});
	}
}
